package com.twotiger.and.util;

import android.app.Activity;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.bean.Share;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.RenrenSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

public class UmShearUtils {

    /**
     * 新分享方法
     * 
     * @param activity
     * @param mShare
     * @param isWeb
     */
    public static void shear(final Activity activity, Share mShare, boolean isweb) {

        String Url = mShare.getUrl();
        String title = mShare.getTitle();
        String cont = mShare.getContent();
        String pic = mShare.getPic();
        String channel = mShare.getChannel(); //分享渠道（weixin:微信、朋友圈；all：所有）

        pic = Constants.HOST + pic; //将图片地址拼接完整

        //QQ分享
        UMQQSsoHandler qqHandler = new UMQQSsoHandler(activity, Constants.QQ_APP_ID,
            Constants.QQ_APP_KEY);
        qqHandler.setTargetUrl(Url);
        qqHandler.addToSocialSDK();
        QQShareContent qqShareContent = new QQShareContent();
        //设置分享文字
        qqShareContent.setShareContent(cont + Url);
        //设置分享title
        qqShareContent.setTitle(title);
        //设置分享图片
        qqShareContent.setShareImage(new UMImage(activity, pic));
        //设置点击分享内容的跳转链接
        qqShareContent.setTargetUrl(Url);

        //Qzone分享
        QZoneSsoHandler handler = new QZoneSsoHandler(activity, Constants.QQ_APP_ID,
            Constants.QQ_APP_KEY);
        handler.setTargetUrl(Url);
        handler.addToSocialSDK();
        QZoneShareContent qzone = new QZoneShareContent();
        //设置分享文字
        qzone.setShareContent(cont + Url);
        //设置点击消息的跳转URL
        qzone.setTargetUrl(Url);
        //设置分享内容的标题
        qzone.setTitle(title);
        //设置分享图片
        qzone.setShareImage(new UMImage(activity, pic));

        //微信分享
        UMWXHandler umwxHandler = new UMWXHandler(activity, Constants.WX_APP_ID,
            Constants.WX_APP_KEY);
        //        umwxHandler.setTitle(title);
        //        umwxHandler.setTargetUrl(Url);
        umwxHandler.addToSocialSDK();
        //设置微信好友分享内容
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        //设置分享文字
        weixinContent.setShareContent(cont + Url);
        //设置title
        weixinContent.setTitle(title);
        //设置分享内容跳转URL
        weixinContent.setTargetUrl(Url);
        //设置分享图片
        UMImage umImage = new UMImage(activity, pic);
        weixinContent.setShareImage(umImage);

        //微信朋友圈
        UMWXHandler wxcircleHandler = new UMWXHandler(activity, Constants.WX_APP_ID,
            Constants.WX_APP_KEY);
        wxcircleHandler.setToCircle(true);
        //        wxcircleHandler.setTitle(content);
        //        wxcircleHandler.setTargetUrl(Url);
        wxcircleHandler.addToSocialSDK();

        //人人
        RenrenSsoHandler renrenSsoHandler = new RenrenSsoHandler(activity, Constants.RENREN_APP_ID,
            Constants.RENREN_APP_KEY, Constants.RENREN_SERCRET_KEY);
        //设置微信朋友圈分享内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(cont + Url);
        //设置朋友圈title
        circleMedia.setTitle(title);
        circleMedia.setShareImage(umImage);
        circleMedia.setTargetUrl(Url);

        UMSocialService shearboard = UMServiceFactory.getUMSocialService("umeng.share");
        if (isweb) {
            shearboard.setShareContent(cont + Url);
            shearboard.setShareMedia(weixinContent); //微信分享
            shearboard.setShareMedia(circleMedia); //朋友圈分享
            shearboard.setShareMedia(qqShareContent);//QQ分享
            shearboard.setShareMedia(qzone);//Qzone分享

            LogUtil.info("分享内容" + cont + Url);
        } else {
            shearboard.setShareContent(cont + Url);
        }
        shearboard.setAppWebSite(SHARE_MEDIA.RENREN, Url);

        //        shearboard.getConfig().setSsoHandler(new SinaSsoHandler());
        shearboard.getConfig().setSsoHandler(renrenSsoHandler);
        //设置分享的图片
        shearboard.setShareImage(umImage);
        //设置在分享面板上显示的平台顺序
        if ("weixin".equals(channel)) {
            shearboard.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE);
        } else if ("all".equals(channel)) {
            shearboard.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.TENCENT, SHARE_MEDIA.SINA,
                SHARE_MEDIA.RENREN);
        } else {
            shearboard.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.TENCENT, SHARE_MEDIA.SINA,
                SHARE_MEDIA.RENREN);
        }
        shearboard.getConfig().closeToast();
        shearboard.openShare(activity, new SnsPostListener() {

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {

            }
        });

    }

    /**
     * 旧分享方法
     * 
     * @param activity
     * @param Url
     * @param isweb
     * @param title
     * @param cont
     * @param pic
     */
    public static void shear(final Activity activity, String Url, boolean isweb, String title,
                             String cont, String pic) {

        pic = Constants.HOST + pic; //将图片地址拼接完整

        //QQ分享
        UMQQSsoHandler qqHandler = new UMQQSsoHandler(activity, Constants.QQ_APP_ID,
            Constants.QQ_APP_KEY);
        qqHandler.setTargetUrl(Url);
        qqHandler.addToSocialSDK();
        QQShareContent qqShareContent = new QQShareContent();
        //设置分享文字
        qqShareContent.setShareContent(cont + Url);
        //设置分享title
        qqShareContent.setTitle(title);
        //设置分享图片
        qqShareContent.setShareImage(new UMImage(activity, pic));
        //设置点击分享内容的跳转链接
        qqShareContent.setTargetUrl(Url);

        //Qzone分享
        QZoneSsoHandler handler = new QZoneSsoHandler(activity, Constants.QQ_APP_ID,
            Constants.QQ_APP_KEY);
        handler.setTargetUrl(Url);
        handler.addToSocialSDK();
        QZoneShareContent qzone = new QZoneShareContent();
        //设置分享文字
        qzone.setShareContent(cont + Url);
        //设置点击消息的跳转URL
        qzone.setTargetUrl(Url);
        //设置分享内容的标题
        qzone.setTitle(title);
        //设置分享图片
        qzone.setShareImage(new UMImage(activity, pic));

        //微信分享
        UMWXHandler umwxHandler = new UMWXHandler(activity, Constants.WX_APP_ID,
            Constants.WX_APP_KEY);
        //        umwxHandler.setTitle(title);
        //        umwxHandler.setTargetUrl(Url);
        umwxHandler.addToSocialSDK();
        //设置微信好友分享内容
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        //设置分享文字
        weixinContent.setShareContent(cont + Url);
        //设置title
        weixinContent.setTitle(title);
        //设置分享内容跳转URL
        weixinContent.setTargetUrl(Url);
        //设置分享图片
        UMImage umImage = new UMImage(activity, pic);
        weixinContent.setShareImage(umImage);

        //微信朋友圈
        UMWXHandler wxcircleHandler = new UMWXHandler(activity, Constants.WX_APP_ID,
            Constants.WX_APP_KEY);
        wxcircleHandler.setToCircle(true);
        //        wxcircleHandler.setTitle(content);
        //        wxcircleHandler.setTargetUrl(Url);
        wxcircleHandler.addToSocialSDK();

        //人人
        RenrenSsoHandler renrenSsoHandler = new RenrenSsoHandler(activity, Constants.RENREN_APP_ID,
            Constants.RENREN_APP_KEY, Constants.RENREN_SERCRET_KEY);
        //设置微信朋友圈分享内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(cont + Url);
        //设置朋友圈title
        circleMedia.setTitle(title);
        circleMedia.setShareImage(umImage);
        circleMedia.setTargetUrl(Url);

        //微博
        UMSocialService shearboard = UMServiceFactory.getUMSocialService("umeng.share");
        if (isweb) {
            shearboard.setShareContent(cont + Url);
            shearboard.setShareMedia(weixinContent); //微信分享
            shearboard.setShareMedia(circleMedia); //朋友圈分享
            shearboard.setShareMedia(qqShareContent);//QQ分享
            shearboard.setShareMedia(qzone);//Qzone分享

            LogUtil.info("分享内容" + cont + Url);
        } else {
            shearboard.setShareContent(cont + Url);
        }
        shearboard.setAppWebSite(SHARE_MEDIA.RENREN, Url);

        //        shearboard.getConfig().setSsoHandler(new SinaSsoHandler());
        shearboard.getConfig().setSsoHandler(renrenSsoHandler);
        //设置分享的图片
        shearboard.setShareImage(umImage);
        //设置在分享面板上显示的平台顺序
        shearboard.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
            SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.TENCENT, SHARE_MEDIA.SINA,
            SHARE_MEDIA.RENREN);
        shearboard.getConfig().closeToast();
        shearboard.openShare(activity, new SnsPostListener() {

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {

            }
        });

    }
}
